﻿using System;
using System.Collections.Generic;

namespace SmartSpace.Silverlight
{
    public class AppView : SlView
    {
        private List<Guid> m_authorizedActions;
        private List<AppAction> m_actions = new List<AppAction>();

        #region properties

        /// <summary>
        /// 视图编号
        /// </summary>
        public Guid ID { get; private set; }

        /// <summary>
        /// 视图名称
        /// </summary>
        public string Name { get; private set; }
        /// <summary>
        /// 获取视图中的操作列表
        /// </summary>
        public List<AppAction> Actions
        {
            get
            {
                return m_actions;
            }
        }

        public Dictionary<string, object> Arguments { get; private set; }

        public int ViewID { get; private set; }

        public string m_appViewCaption;

        #endregion

        public AppView()
            :this(Guid.Empty,"Null View")
        {
        }

        public void SetViewID(int viewID)
        {
            ViewID = viewID;
        }

        public virtual void GoToRecordView(SlView parentView, int recordID)
        {
        }

        /// <summary>
        /// 构造视图对象
        /// </summary>
        /// <param name="id">编号</param>
        /// <param name="name">名称</param>
        public AppView(Guid id, string name)
        {
            ID = id;
            Name = name;
        }

        public void SetCaption(string caption)
        {
            m_appViewCaption = caption;
        }

        public override string GetCaption()
        {
            return this.m_appViewCaption;
        }

        /// <summary>
        /// 设置被授权的操作编号
        /// </summary>
        /// <param name="authorizedActions"></param>
        public void SetAuthorizedActions(List<Guid> authorizedActions)
        {
            m_authorizedActions = authorizedActions;
        }

        /// <summary>
        /// 设置参数
        /// </summary>
        /// <param name="arguments"></param>
        public void SetArguments(Dictionary<string, object> arguments)
        {
            Arguments = arguments;
        }

        /// <summary>
        /// 注册RegisterAction
        /// </summary>
        /// <param name="action">操作</param>
        protected void RegisterAction(AppAction action)
        {
            m_actions.Add(action);
        }

        /// <summary>
        /// 验证授权的操作
        /// </summary>
        /// <param name="authorizedActions"></param>
        private void ValidateActions(List<Guid> authorizedActions)
        {
            if (authorizedActions == null)
            {
                return;
            }

            foreach (AppAction action in m_actions)
            {
                bool isAuthorized = false;
                foreach (Guid authorizedID in authorizedActions)
                {
                    if (action.ID == authorizedID)
                    {
                        isAuthorized = true;
                        break;
                    }
                }
                action.Enable(isAuthorized);
            }
        }

        protected sealed override void OnShown()
        {
            if (IsFirstDisplay)
            {
                // 验证权限
                ValidateActions(m_authorizedActions);
                
            }
            OnExtendShown();
        }

        protected virtual void OnExtendShown()
        {
        }
    }
}
